package com.atguigu.userprofile.util;

import com.atguigu.userprofile.constant.ConstCode;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ClickhouseUtil {

    public static void executeSql(String sql ) {
        Connection connection = null ;
        PreparedStatement preparedStatement = null ;
        try {
            Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
            String url = MyPropsUtil.get(ConstCode.CLICKHOUSE_URL);
            connection = DriverManager.getConnection(url);
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.execute() ;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Clickhouse执行SQL失败....");
        }finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException("Clickhouse关闭资源失败....");
            }
        }
    }
}
